package com.hz.urlcert;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.hz.urlcert.tls.HttpClient;

import java.io.IOException;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void clickBtn(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                test();
            }
        }).start();
    }

    private void test() {
        try {
            HttpClient httpClient = setHttpClient("https://m1.cmbc.com.cn:8003/CMBC_MBServer/scripts/fidoServer/facets.uaf");

            if (httpClient.getStatusCode() == 200) {
                showMsg("证书校验通过");
            } else {
                showMsg("证书校验失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            showMsg("exception:" + e.getMessage());
        }
    }

    private void showMsg(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 请求类
     */
    @NonNull
    private static HttpClient setHttpClient(String appid) throws IOException {
        HttpClient httpClient = new HttpClient(appid, HttpClient.HttpMethod.GET);
        String[] supports = {"SSLv3", "TLSv1", "TLSv1.2", "TLSv1.1"};
        httpClient.setAllowedSSLProtocols(supports);
        httpClient.setFollowRedirects(false);
//        httpClient.setMyHostnameVerifier();
//        httpClient.setMyX509TrustManager();
        httpClient.addHeader("Accept", "application/fido.trusted-apps+json");
        httpClient.sendRequest();
        return httpClient;
    }

}